package com.bdc.HBase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * HBase：根据Rowkey批量查询数据
 */
public class HBaseRowKey {

    public static Configuration conf = null;
    public static Connection connection = null;
    public static Admin admin = null;

    static {
        conf = HBaseConfiguration.create();

        conf.set("hbase.zookeeper.quorum", "xxxxxxx,xxxxx");
        conf.set("hbase.master", "xxxxx:60010");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conf.set("zookeeper.znode.praent", "/hbase-unsecure");
        conf.set("hbase.client.keyvalue.maxsize", "1048576000");
        conf = HBaseConfiguration.create(conf);
        try {
            connection = ConnectionFactory.createConnection(conf);
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static List<String> queryTableTestBatch(List<String> rowkeyList) throws IOException {
        List<Get> getList = new ArrayList();
        List list = new ArrayList();
        String tableName = "table_a";
        //获取表
        Table table = connection.getTable(TableName.valueOf(tableName));
        //把rowkey加到get里，再把get装到List中
        for (String rowkey : rowkeyList) {
            Get get = new Get(Bytes.toBytes(rowkey));
            getList.add(get);
        }
        //重点在这，直接查getList<Get>
        Result[] results = table.get(getList);
        for (Result result : results) {
            //对返回的结果集进行操作
            for (Cell kv : result.rawCells()) {
                String value = Bytes.toString(CellUtil.cloneValue(kv));
                list.add(value);
            }
        }
        return list;
    }


}
